package cn.ytr.crm.web.controller;

import cn.ytr.crm.domain.Department;
import cn.ytr.crm.query.QueryObject;
import cn.ytr.crm.service.IDepartmentService;
import cn.ytr.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/department")
@Controller
public class DepartmentController {

	@Autowired
	IDepartmentService departmentService;

	/**
	 * 显示 部门列表页
	 */
	@RequiresPermissions(value = {"访问:部门列表页面", "department:list"}, logical = Logical.OR)
	//@RequiredPermission(name = "访问部门列表页面", expression = "department:list")
	@RequestMapping("/list")
	public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
		//用分页插件
		model.addAttribute("pageInfo", departmentService.query(qo));
		return "department/list";
	}

	/**
	 * 处理 ajax异步 去增加或编辑部门页提交的
	 */
	@RequiresPermissions(value = {"操作:新增或修改部门", "department:saveOrUpdate"}, logical = Logical.OR)
	//@RequiredPermission(name = "操作新增或修改部门", expression = "department:saveOrUpdate")
	@RequestMapping("/saveOrUpdate")
    @ResponseBody
	public JsonResult saveOrUpdate(Department department) {
		JsonResult jsonResult = new JsonResult();
		try {
			departmentService.saveOrUpate(department);
		    return jsonResult;
		} catch (Exception e) {
		    jsonResult.mark("服务器提示：操作出错");
		    e.printStackTrace();
		    return jsonResult;
		}

	}

	/**
	 * 处理 删除部门，需要维护员工表
	 */
    @ResponseBody
    @RequiresPermissions(value = {"操作:删除部门", "department:delete"}, logical = Logical.OR)
	//@RequiredPermission(name = "操作删除部门", expression = "department:delete")
	@RequestMapping("/delete")
	public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
			departmentService.delete(id);
            return jsonResult;
        } catch (Exception e) {
            jsonResult.mark("服务器提示：操作出错");
            e.printStackTrace();
            return jsonResult;
        }

	}

}
